package logic

import (
	"context"
	"fmt"
	"go.uber.org/zap"
	"order_srv/internal/dao"
	"order_srv/internal/svc"
	"order_srv/order"
	"smart-health-platforms/pkg"
	"smart-health-platforms/untils"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderAddBargainLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewOrderAddBargainLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderAddBargainLogic {
	return &OrderAddBargainLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 砍价订单
func (l *OrderAddBargainLogic) OrderAddBargain(in *order.OrderAddBargainRequest) (*order.OrderAddBargainResponse, error) {
	alipay := untils.AliPay{}
	bargain, err := dao.GetBargain(int32(in.BargainId), int32(in.AddressId), in.RealName, in.UserPhone, in.PayType)
	if err != nil {
		pkg.Error("失败", zap.Error(err))
		return nil, err
	}

	return &order.OrderAddBargainResponse{
		OrderSn: bargain.OrderSn,
		PayUrl:  alipay.AliPay(bargain.OrderSn, fmt.Sprintf("%.2f", bargain.TotalPrice)),
	}, nil
}
